﻿using Mapster;
using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.JurMunu;
using Shop.Model.Entity.Jur;
using Shop.Model.View.Admin.JurMunu;
using Shop.Model.View.Admin.JurUser;

namespace Shop.Services.Admin;

public class JurMunuService: IJurMunuService
{
    private readonly RepositoryBase<JurMenu> _menuRepository;

    public JurMunuService(RepositoryBase<JurMenu> menuRepository)
    {
        _menuRepository = menuRepository;
    }

    /// <summary>
    /// 添加菜单
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult> AddMenu(MenuAddDto dto)
    {
        if (dto.ParentId > 0)
        {
            var exit = await _menuRepository.AnyAsync(x => x.Id == dto.ParentId);
            if (!exit)
                throw new Exception("菜单上级不存在");
        }
        var data = dto.Adapt<JurMenu>();
        data.CreateTime = DateTime.Now;
        await _menuRepository.InsertAsync(data);
        return new AppResult();
    }

    /// <summary>
    /// 查询菜单
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult<List<MenuListVo>>> GetMenuList()
    {
        return await _menuRepository.AsQueryable().Where(x => x.State == 0)
            .Select(x => new MenuListVo
            {
                Id = x.Id,
                ParentId = x.ParentId,
                Authority = x.Authority,
                Component = x.Component,
                MenuName = x.MenuName,
                MenuType = x.MenuType,
                Path = x.Path,
                CreateTime = x.CreateTime
            }).ToListAsync();
    }

    /// <summary>
    /// 菜单树
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult<MenuTreeVo[]>> GetMenuTree()
    {
        var data = await _menuRepository.AsQueryable().Where(x => x.State == 0)
            .Select<MenuTreeVo>().ToListAsync();
        var result = data.FindAll(x => x.ParentId == 0).ToArray();
        foreach (var item in result)
        {
            if(item.MenuType == 0)
            {
                var children = data.FindAll(x => x.ParentId == item.Id).ToArray();
                foreach (var child in children)
                {
                    if (child.MenuType == 0)
                        child.Children = data.FindAll(x => x.ParentId == child.Id).ToArray();
                }
                item.Children = children;
            }
        }
        return result;
    }

    /// <summary>
    /// 编辑菜单
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult> Modify(MenuModifyDto dto)
    {
        var menu = await _menuRepository.GetByIdAsync(dto.Id);
        if (menu == null)
            throw new Exception("找不到菜单");
        if (menu.ParentId != dto.ParentId && dto.ParentId > 0)
        {
            var exit = await _menuRepository.AnyAsync(x => x.Id == dto.ParentId);
            if (exit)
                throw new Exception("菜单的上级菜单不存在");
        }
        menu.MenuName = dto.MenuName;
        menu.MenuType = dto.MenuType;
        menu.Path = dto.Path;
        menu.Authority = dto.Authority;
        menu.Component = dto.Component;
        menu.ParentId = dto.ParentId;
        menu.UpdateTime = DateTime.Now;
        await _menuRepository.UpdateAsync(menu);
        return new AppResult();
    }

    /// <summary>
    /// 获取文件夹列表
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult<SimpleTreeVo<long>[]>> GetFileList()
    {
        var data = _menuRepository.Where(x => x.MenuType == 0 && x.State == 0).ToList();
        var result =  data.FindAll(x => x.ParentId == 0).Select(x => new SimpleTreeVo<long> { Value = x.Id, Label = x.MenuName }).ToArray();
        foreach (var item in result)
            item.Children = data.FindAll(x => x.ParentId == item.Value).Select(x => new SimpleTreeVo<long> { Value = x.Id, Label = x.MenuName }).ToArray();
        return result;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="uId"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<AppResult> Delete(long uId)
    {
        var menu = await _menuRepository.GetByIdAsync(uId);
        if (menu == null)
            throw new Exception("参数错误，找不到数据");
        menu.State = -1;
        menu.UpdateTime = DateTime.Now;
        await _menuRepository.UpdateAsync(menu);
        return new AppResult();
    }
}